home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / ai.prl / mike1.exe / TOILET.KB < prev    next >
Encoding:
Text File  |  1990-07-11  |  4.1 KB  |  127 lines

  1. /* TOILET.KB */
  2. /* Flush toilet simulation */
  3. /* Load this knowledge base from MIKE as follows:
  4.     ?- kb 'toilet.kb'.
  5.  
  6. Run the simulation by invoking the forward chainer as follows:
  7.     ?- fc.
  8. */
  9. /* There are only 3 elements:
  10. float1, tank1, and pipe1.  pipe1 starts off with a flow rate of 5,
  11. and tank1 starts off at level 0.  We only do integer
  12. arithmetic: as the float rises, the flow rate decreases
  13. from 5 to 3 to 1 to 1 (again, due to integer division), then to 0.
  14. At the same time,
  15. the float1 height goes from 0 to 5 to 8 to 9 to 10.
  16. Flow rate is simply (hard wired) to be 5 - (FloatHeight//2)
  17. so at the end of each respective cycle of the rule interpreter
  18. this is (5 - (0//2)) and then (5 - (5//2) and then (5 - (8//2)) and then
  19. (5 - (9//2)) and then finally (5 - (10//2)) when it stops.
  20. */
  21.  
  22. /* first rule clears 'start' symbol from working memory, then
  23. dynamically alters the conflict resolution strategy so that
  24. only refractoriness is used (and not specificity or recency).
  25. */
  26.  
  27. rule init forward
  28.   if
  29.     start    /* special symbol which is always in WM at start */
  30.   then
  31.     remove start &
  32.     strategy [refractoriness] &  /* SPECIFITY IS TABOO HERE */
  33.       /* so is recency in this context (this differs slightly from text) */
  34. /* the following is a good example of the difference between note and
  35.    add.  note acts on frame memory and its effects are both permanent
  36.    and it will overwrite the preceding contents.  Its effects are therefore
  37.    destructive.  add on the otherhand adds the new term to working memory.
  38.    Any previous terms are not removed, and the effects of working memory
  39.    are cleaned up on every run of the rule interpreter */
  40.     note the level of tank1 is 0 &
  41.     add tank_filling.  /* begin with empty tank, but fill it up first */
  42.  
  43.  
  44. rule stop_it forward
  45.   if
  46.     tank_filling &
  47.     the level of tank1 > 9   /* will eventually reach this */
  48.   then
  49.     strategy [refractoriness, recency, specificity] & /* restore defaults */
  50.     halt .
  51.  
  52. rule filling forward
  53.   if
  54.      tank_filling &
  55.      the level of tank1 is L &
  56.      the flow_rate of pipe1 is R
  57.   then
  58.      prolog(NewLevel is L + R) &  /* must call prolog to do sums */
  59.      note the level of tank1 is NewLevel.  /* this triggers change_rule */
  60.       /* this OVERWRITES former level of tank1 !!!!! */
  61.  
  62.  
  63. tank1 instance_of tank with
  64.   height: 5,
  65.   width: 4,
  66.   depth: 3,
  67.   regulator: float1.
  68.  
  69. float1 instance_of float with
  70.   arm_length: 4,
  71.   regulated_inlet: pipe1.
  72.  
  73. pipe1 instance_of pipe with
  74.   thickness: 5,
  75.   comes_from: mains_supply,
  76.   goes_to: tank1,
  77.   flow_rate: 5.  /* starting flow_rate is 5, gets changed later */
  78.  
  79.  
  80. pipe subclass_of flow_through_device with
  81.   material: copper.
  82.  
  83. tank subclass_of vessel with
  84. /* next slot not used at moment, but illustrates the syntax */
  85.  volume:
  86.   [value: V,
  87.    access_rule: (if the height of ?self is H &
  88.                     the width of ?self is W &
  89.                     the depth of ?self is D &
  90.                     prolog(V is H*W*D)
  91.                 then
  92.                     make_value V)],
  93.  level :
  94.   [value: L,
  95.    change_rule : (if the regulator of ?self is Reg
  96.                   then
  97.                      prolog(draw_image('level of tank 1',L)) &
  98.                      note the height of Reg is L)].
  99.  
  100. float subclass_of regulating_feedback_valves with
  101.   arm_length: AL,
  102.   regulated_inlet: P,
  103.   height :
  104.    [value: H,
  105.     change_rule: (if the regulated_inlet of ?self is Inlet /* e.g. pipe1 */
  106.                   then
  107.                      prolog(NewFlowRate is 5 - (H//2)) &
  108.                      note the flow_rate of Inlet is NewFlowRate)].
  109.  
  110.  
  111. /* draw image -- just a TTY version... here is the
  112.   place to interface you own local graphics routines.  This one
  113.   just draws a few asterisks on the display
  114. */
  115.  
  116. draw_image(Text,Number) :-
  117.  nl,
  118.  write('------------ '),write(Text),write(' ---------------'),nl,
  119.  tty_image(Number),nl.
  120.  
  121. /* the terminationg conditions are for compatibility with other Prologs */
  122. tty_image(Number):- 0 >= Number.
  123. tty_image(N) :-
  124.   write('*'),
  125.   N1 is N - 1,
  126.   tty_image(N1).
  127.